﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using Internet_Cafe.App_Code;

namespace Internet_Cafe
{
    /// <summary>
    /// Interaction logic for ComputerItem.xaml
    /// </summary>
    public partial class ComputerItem : UserControl
    {
        public string ID { set; get; }
        DateTime actived;
        DateTime deactived;
        int minutes;
        bool isPostpaid { set; get; }
        public bool isActive { set; get; }
        public ComputerItem(string ip,string id)
        {
            InitializeComponent();
            txtIP.Text = ip;
            ID = id;
            isActive = false;
            txtTimeInfo.Text = "Неактивен";
            imgIcon.Source = MethodsCollection.ConvertBitmapToBitmapSource(Properties.Resources.pcstyle1);
        }

        public void setInactive()
        {
            imgIcon.Source = MethodsCollection.ConvertBitmapToBitmapSource(Properties.Resources.pcstyle1);
            isActive = false;
            txtTimeInfo.Text = "Неактивен";
            
            if (isPostpaid)
            {
                deactived = DateTime.Now;
                App_Code.DatabaseModel.Bill bill = new App_Code.DatabaseModel.Bill();
                //minutes = deactived.Subtract(actived).Minutes;
                bill.ActiveStart = actived;
                bill.ActiveEnd = deactived;
                bill.ComputerID = this.ID;
                bill.WorkerID = AppHelper.LoggedUser.user.ID;
                bill.Cost = AppHelper.GetCost(minutes);
                Bill b = new Bill(bill);
                b.ShowDialog();
                //stavi smetka so cena minutes*cena
            }
            else
            {
                App_Code.DatabaseModel.Bill bill = new App_Code.DatabaseModel.Bill();
                bill.ActiveStart = actived;
                bill.ActiveEnd = deactived;
                bill.ComputerID = this.ID;
                bill.WorkerID = AppHelper.LoggedUser.user.ID;
                bill.Cost = AppHelper.GetCost(minutes);
                Bill b = new Bill(bill);
                b.ShowDialog();
            }
        }

        public void setActivePostpaid(DateTime dt)
        {
            actived = dt;
            isPostpaid = true;
            isActive = true;
            txtTimeInfo.Text = "Ативен од: " + String.Format("{0:t}", dt);
            imgIcon.Source = MethodsCollection.ConvertBitmapToBitmapSource(Properties.Resources.pcstyle2);
        }
        public void setActivePrepaid(DateTime dt)
        {
            isPostpaid = false;
            isActive = true;
            actived = DateTime.Now;
            deactived = dt;
            minutes = dt.Subtract(actived).Minutes;
            DateTime activeto = DateTime.Now.AddMinutes((double)minutes);
            txtTimeInfo.Text = "Активен до:" + String.Format("{0:t}", dt);
            imgIcon.Source = MethodsCollection.ConvertBitmapToBitmapSource(Properties.Resources.pcstyle2);
        }
    }
}
